/*
 * This file is part of TradeHelper library.
 *
 * TradeHelper library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TradeHelper library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TradeHelper library.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.rag.assistants;

import com.alee.extended.panel.CenterPanel;
import com.alee.extended.window.TestFrame;
import com.alee.laf.WebLookAndFeel;
import com.alee.laf.label.WebLabel;
import com.alee.laf.panel.WebPanel;
import com.alee.utils.ImageUtils;
import com.rag.core.Item;
import com.rag.core.ItemType;
import com.rag.core.ServerType;
import com.rag.managers.BaseManager;
import com.rag.managers.RagManager;
import com.rag.utils.RagUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * User: mgarin Date: 14.06.12 Time: 18:32
 */

public class ItemsIconLoader
{
    private static int startId = 0;
    private static ServerType serverType = ServerType.iro;

    public static void main ( String[] args )
    {
        WebLookAndFeel.install ();
        BaseManager.initialize ();
        RagManager.initialize ();

        new TestFrame ( new CenterPanel ( new WebPanel ( new FlowLayout () )
        {
            {
                final WebPanel panel = this;
                new Thread ( new Runnable ()
                {
                    public void run ()
                    {
                        for ( Item item : RagManager.getKnownItems ().getValues () )
                        {
                            if ( !RagUtils.isCard ( item ) &&
                                    Integer.parseInt ( item.getId () ) > startId )
                            {
                                ImageIcon icon = null;
                                try
                                {
                                    icon = getItemIcon ( item );
                                    BufferedImage bi = ImageUtils.getBufferedImage ( icon );
                                    if ( bi != null )
                                    {
                                        ImageIO.write ( bi, "png",
                                                new File ( "remote/icons/items/" + item.getId () +
                                                        ".png" ) );
                                        //                                        ImageIO.write ( bi, "png", new File (
                                        //                                                "src/com/rag/core/icons/items/" + item.getId () +
                                        //                                                        ".png" ) );
                                        System.out.println ( "Done: " + item.getId () );
                                    }
                                    else
                                    {
                                        System.err.println ( "Failed: " + item.getId () );
                                    }
                                }
                                catch ( Throwable e )
                                {
                                    System.err.println (
                                            "Failed: " + item.getId () + " (" + e.getMessage () +
                                                    ")" );
                                }

                                panel.add ( new WebPanel ( true, new WebLabel ( icon ) ) );
                                if ( panel.getComponentCount () > 20 )
                                {
                                    panel.remove ( 0 );
                                }
                                panel.revalidate ();
                            }
                        }
                        System.exit ( 0 );
                    }
                } ).start ();
            }
        } ) )
        {
            {
                setSize ( 800, 200 );
                setLocationRelativeTo ( null );
            }
        };
    }

    public static ImageIcon getItemIcon ( Item item )
    {
        String key = item.getId ();
        try
        {
            // Выбираем сервер, с которого грузить иконки итемов
            String address;
            if ( serverType.equals ( ServerType.motr ) )
            {
                // MOTR
                if ( RagUtils.isCard ( item ) )
                {
                    // Для карточек - статичный ID
                    address = "http://85.17.167.159/dbpic/item2/" + "4001" + ".png";
                }
                else
                {
                    // Для прочих итемов их личныйID
                    address = "http://85.17.167.159/dbpic/item2/" + key + ".png";
                }
            }
            else if ( serverType.equals ( ServerType.iro ) )
            {
                // IroWiki
                address = "http://db.irowiki.org/image/item/" + key + ".png";
            }
            else
            {
                // RMS
                address = "http://file5.ratemyserver.net/items/small/" +
                        ( item.getItemType ().equals ( ItemType.card ) ? "card" : key ) +
                        ".gif";
            }

            // Загружаем иконку
            return ImageUtils.loadImage ( new URL ( address ) );

            //            // Создаём плэйсхолдер-иконку
            //            if ( imageIcon == null || imageIcon.getImage () == null )
            //            {
            //                BufferedImage image = new BufferedImage ( 24, 24, BufferedImage.TYPE_INT_ARGB );
            //                Graphics2D g2d = image.createGraphics ();
            //                g2d.setPaint ( Color.GRAY );
            //                g2d.drawRoundRect ( 0, 0, 23, 23, 4, 4 );
            //                g2d.drawLine ( 2, 2, 21, 21 );
            //                g2d.drawLine ( 21, 2, 2, 21 );
            //                g2d.dispose ();
            //                imageIcon = new ImageIcon ( image );
            //            }
            //
            //            // Возвращаем иконку
            //            return imageIcon;
        }
        catch ( MalformedURLException e )
        {
            return null;
        }
    }
}